package cn.itcast.order.service;

import cn.itcast.order.mapper.OrderMapper;
import cn.itcast.order.pojo.Order;

import com.itheima.feign.clients.UserClient;
import com.itheima.feign.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OrderService {

    @Autowired
    RestTemplate restTemplate;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    UserClient userClient;

    public Order queryOrderById(Long orderId) {
        userClient.getUser(1L, "itcast");

        // 1.查询订单
        Order order = orderMapper.findById(orderId);

        //2.构建url 调用远程接口
//        Long userId = order.getUserId();
//        String url = "http://user-service/user/"+ userId;
//        ResponseEntity<User> entity = restTemplate.getForEntity(url, User.class);
        //2.调用远程接口
        User user = userClient.findById(order.getUserId());

        //3.封装user
//        order.setUser(entity.getBody());
        order.setUser(user);

        // 4.返回
        return order;
    }
}
